/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.advertisements_tag;

import com.apt.bean.Advertisements;
import com.apt.bean.User;
import com.apt.tag.Process_Page;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bach
 */
public class Advertisement_TagHandler extends SimpleTagSupport {

    private String page;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        int cursor1;
        HashMap<Integer, Advertisements> hm = Get_Advertisements.getListAdvertisements();
        try {
            if (hm != null && hm.size() != 0) {
                out.println(Process_Page.divPage(page, hm.size(), 3));
                int uid_f = 1;
                int uid_l = 30;

                try {
                    cursor1 = Integer.parseInt(page);

                } catch (NumberFormatException ex) {
                    cursor1 = 1;
                }
                if (cursor1 > 1) {
                    uid_f = (cursor1 - 1) * 30;
                    uid_l = cursor1 * 30;
                }

                int current_page = uid_l / 30;

                for (int i = uid_f; i < uid_l; i++) {
                    Advertisements u = hm.get(i);
                    if (u != null) {
                        int status = Integer.parseInt(u.getAdvertisement_Status());

                        StringBuilder sb = new StringBuilder(" <tr>");
                        //sb.append(" <td><input type='checkbox' /></td>");
                        sb.append("<td>" + u.getCompany_Issued() + "</td>");
                        sb.append("  <td><a href=\"#\" title=\"title\"> view Image</a></td>");
                        sb.append("   <td>" + u.getCreated_Date() + "</td>");
                        sb.append("   <td>" + u.getExpired_Date() + "</td>");
                        if (status == 1) {
                            sb.append("   <td> Enable</td>");
                            sb.append("  <td><a href=\"DisableAdvServlet?advertisement_Id=" + u.getAdvertisement_Id() + "&pageno=" + current_page +"&imageId="+u.getImage_Id()+ "\" title=\"Disable\"><img src=\"resources/images/icons/cross.png\" alt=\"Disable\" /></a>");
                            sb.append("   </td>");
                        } else {
                            sb.append("   <td> Disable</td>");
                            sb.append("  <td><a href=\"EnableAdvServlet_adv?advertisement_Id=" + u.getAdvertisement_Id() + "&pageno=" + current_page +"&imageId="+u.getImage_Id()+ "\" title=\"Disable\"><img src=\"resources/images/icons/pencil.png\" alt=\"Disable\" /></a>");
                            sb.append("   </td>");
                        }
                        sb.append(" </tr>");

                        out.print(sb.toString());

                    }
                }

            }


        } catch (IOException ex) {
            Logger.getLogger(Advertisement_TagHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setPage(String page) {
        this.page = page;
    }
}
